package com.briup.jz.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.briup.jz.bean.CertificationApply;
import com.briup.jz.bean.CertificationApplyExample;

import com.briup.jz.dao.CertificationApplyMapper;
import com.briup.jz.service.ICertificationApplyService;
import com.briup.jz.utils.CustomerException;
@Service
public class CertificationApplyServiceImpl implements ICertificationApplyService {

	@Autowired
	private CertificationApplyMapper certificationApplyMapper;

	@Override
	public void saveOrUpdate(CertificationApply certificationApply) throws CustomerException {
		if(certificationApply.getId()!=null) {
			certificationApplyMapper.updateByPrimaryKey(certificationApply);
		} else {
			// 判断是否有同名的分类，如果有抛出异常
			CertificationApplyExample example = new CertificationApplyExample();
			example.createCriteria().andRealnameEqualTo(certificationApply.getRealname());
			List<CertificationApply> list = certificationApplyMapper.selectByExample(example);
			if(list.size()>0) {
				throw new CustomerException("同名的分类已存在");
			}
			certificationApplyMapper.insert(certificationApply);
		}
	}

	@Override
	public List<CertificationApply> query(String name) {
		CertificationApplyExample example = new CertificationApplyExample();
		if(name!=null){
			example.createCriteria().andRealnameLike("%"+name+"%");
		}
		List<CertificationApply> list = certificationApplyMapper.selectByExample(example);
		return list;
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		CertificationApply certificationApply = certificationApplyMapper.selectByPrimaryKey(id);
		if(certificationApply == null) {
			throw new CustomerException("要删除的分类信息不存在");
		}
		certificationApplyMapper.deleteByPrimaryKey(id);
		
	}
}
